package com.codingapi.core.framework.utils;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author modificial
 * @date 2018/5/3 0003
 * @company codingApi
 * @description
 */
public class SerialNumberUtil {
    /**
     * 自定义进制(0,1没有加入,容易与o,l混淆)
     */
    private static final char[] seeds = new char[]{'2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
            'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

    /**
     * 生成用户邀请码
     *
     * @return
     */
    public static String getSerialNumber() {
        final Set<Character> set = new HashSet<>(6);
        while (set.size() < 6) {
            int rnd = ThreadLocalRandom.current().nextInt(31);
            set.add(seeds[rnd]);
        }
        StringBuffer stringBuffer = new StringBuffer();
        set.forEach(it -> stringBuffer.append(it));
        return stringBuffer.toString();
    }

    /**
     * 生成用户唯一标识
     *
     * @return
     */
    public static String getIdentifier() {

        final Set<Character> set = new HashSet<>(8);
        while (set.size() < 8) {
            int rnd = ThreadLocalRandom.current().nextInt(31);
            set.add(seeds[rnd]);
        }
        StringBuffer stringBuffer = new StringBuffer();
        set.forEach(it -> stringBuffer.append(it));
        return stringBuffer.toString();
    }

    /**
     * 随机生成code
     *
     * @return
     */
    public static String getCode() {

        final Set<Character> set = new HashSet<>(10);
        while (set.size() < 6) {
            int rnd = ThreadLocalRandom.current().nextInt(31);
            set.add(seeds[rnd]);
        }
        StringBuffer stringBuffer = new StringBuffer();
        set.forEach(it -> stringBuffer.append(it));
        return stringBuffer.toString();
    }

    /**
     * 指定长度生成code
     *
     * @param length
     * @return
     */
    public static String getCode(int length) {

        final Set<Character> set = new HashSet<>(length);
        while (set.size() < length) {
            int rnd = ThreadLocalRandom.current().nextInt(31);
            set.add(seeds[rnd]);
        }
        StringBuffer stringBuffer = new StringBuffer();
        set.forEach(it -> stringBuffer.append(it));
        return stringBuffer.toString();
    }

}
